package gray.flow;


import java.util.Random;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class PhotoFlowActivity extends Activity {
    private HorizontalScrollView flow;
	private RelativeLayout root;
	private int rowsCount=5;
	private int dynamicRowsCount;
	private boolean lastWas;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        flow=(HorizontalScrollView)findViewById(R.id.flow);
        root=(RelativeLayout)findViewById(R.id.flow_root);
        LayoutInflater inf=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        
        String[] projection = {MediaStore.Images.Media._ID,
				MediaStore.Images.Media.DISPLAY_NAME,
				MediaStore.Images.Media.DATA
				};
		Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,"", null, null);
		cursor.moveToFirst();
		int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
		for(int i=0;i<100;i++){
			cursor.moveToPosition(i);
			// Get the current value for the requested column
			int imageID =cursor.getInt(columnIndex);
		    // obtain the image URI
		    Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
		    String url = uri.toString();
		    // Set the content of the image based on the image URI
		    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
		    View rItem=inf.inflate(R.layout.item_layout,null);
        	ImageView iv=(ImageView) rItem.findViewById(R.id.icon_item);
		    iv.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null));
		    LinearLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
        	lp.height=100+new Random().nextInt(50); 
//        	lp.width=100+new Random().nextInt(50);
        	iv.setLayoutParams(lp);
        	//iv.setImageResource(R.drawable.icon);
        	iv.setScaleType(ScaleType.CENTER_CROP);
        	addToRoot(rItem);
		}
        
//        for(int i=0;i<100;i++){
//        	
//        	
//        	View rItem=inf.inflate(R.layout.item_layout,null);
//        	ImageView iv=(ImageView) rItem.findViewById(R.id.icon_item);
//        	iv.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
//	                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null));
//        	LinearLayout.LayoutParams lp=(LayoutParams) iv.getLayoutParams();
//        	lp.height=100+new Random().nextInt(50); 
//        	lp.width=100+new Random().nextInt(50);
//        	iv.setLayoutParams(lp);
//        	//iv.setImageResource(R.drawable.icon);
//        	//iv.setScaleType(ScaleType.FIT_XY);
//        	addToRoot(rItem);
//        }
        root.postDelayed(new Runnable(){

			@Override
			public void run() {
				root.getWidth();
				
			}
        	
        }, 100);
    }
    private void addToRoot(View v){
    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
    			RelativeLayout.LayoutParams.WRAP_CONTENT);
    	if(root.getChildCount()>0){
    		 
    		v.setId(100+root.getChildCount());
    		if(root.getChildCount()%rowsCount==(rowsCount-1)){
    			View iv=v.findViewById(R.id.icon_item);
    			LinearLayout.LayoutParams lv=new LinearLayout.LayoutParams(iv.getLayoutParams().width, 
    					LinearLayout.LayoutParams.FILL_PARENT);
    			lp.addRule(RelativeLayout.BELOW, root.getChildAt(root.getChildCount()-1).getId());
    			lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
    			if(root.getChildCount()>rowsCount){
    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
    			}
    			iv.setLayoutParams(lv);
    			
    		}else 
			if(root.getChildCount()%rowsCount==0){
				lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
    			//lp.addRule(RelativeLayout.ABOVE, root.getChildAt(root.getChildCount()-1).getId());
    			lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
//    			if(root.getChildCount()>rowsCount){
//    				lp.addRule(RelativeLayout.ABOVE, root.getChildAt(root.getChildCount()-(rowsCount-1)).getId());
//    			}else{
//    				//lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-1).getId());
//    			}
    		}else{
    			lp.addRule(RelativeLayout.BELOW, root.getChildAt(root.getChildCount()-1).getId());
    			if(root.getChildCount()>rowsCount){
    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
    				//lp.addRule(RelativeLayout.ALIGN_BOTTOM, root.getChildAt(root.getChildCount()-(rowsCount+1)).getId());
    			}
    		}
//    		v.measure(MeasureSpec.AT_MOST,MeasureSpec.AT_MOST);//MeasureSpec.makeMeasureSpec(root.getWidth(), MeasureSpec.AT_MOST),
//    				//MeasureSpec.makeMeasureSpec(root.getHeight(), MeasureSpec.AT_MOST));
//    		if(root.getChildCount()>rowsCount){
//    			if(root.getChildAt(root.getChildCount()-rowsCount).getWidth()<root.getChildAt(root.getChildCount()-rowsCount-1).getWidth()){
//    				lp.height=root.getChildAt(root.getChildCount()-rowsCount).getHeight();
//    			}
//    				
//    		}
    		root.addView(v,lp);
    	}else{
    		v.setId(100);
    		root.addView(v);
    	}
    }
    
//    private void addToRoot(View v){
//    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//    	if(root.getChildCount()>0){
//    		 
//    		v.setId(100+root.getChildCount());
//    		if(root.getChildCount()%rowsCount==(rowsCount-1)){
//    			lp.addRule(RelativeLayout.BELOW, root.getChildAt(root.getChildCount()-1).getId());
//    			lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
//    			//lp.height=RelativeLayout.LayoutParams.MATCH_PARENT;
//    			if(root.getChildCount()>rowsCount){
//    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
//    			}
//    			
//    		}else 
//			if(root.getChildCount()%rowsCount==0){
//				lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
//    			//lp.addRule(RelativeLayout.ABOVE, root.getChildAt(root.getChildCount()-1).getId());
//    			lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
////    			if(root.getChildCount()>2){
////    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-2).getId());
////    			}else{
////    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-1).getId());
////    			}
//    		}else{
//    			lp.addRule(RelativeLayout.BELOW, root.getChildAt(root.getChildCount()-1).getId());
//    			if(root.getChildCount()>rowsCount){
//    				lp.addRule(RelativeLayout.RIGHT_OF, root.getChildAt(root.getChildCount()-rowsCount).getId());
//    			}
//    		}
//    		v.measure(MeasureSpec.AT_MOST,MeasureSpec.AT_MOST);//MeasureSpec.makeMeasureSpec(root.getWidth(), MeasureSpec.AT_MOST),
//    				//MeasureSpec.makeMeasureSpec(root.getHeight(), MeasureSpec.AT_MOST));
//    		if(root.getChildCount()>rowsCount){
//    			if(root.getChildAt(root.getChildCount()-rowsCount).getWidth()<root.getChildAt(root.getChildCount()-rowsCount-1).getWidth()){
//    				lp.height=root.getChildAt(root.getChildCount()-rowsCount).getHeight();
//    			}
//    				
//    		}
//    		root.addView(v, lp);
//    	}else{
//    		v.setId(100);
//    		root.addView(v);
//    	}
//    }
}